package com.szx.usercenter.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.szx.usercenter.domain.SysRole;
import com.szx.usercenter.service.SysRoleService;
import com.szx.usercenter.util.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author songzx
 * @date 2024/5/18
 * @apiNote
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {
    @Autowired
    SysRoleService sysRoleService;

    @ApiOperation("根据ID查询角色信息")
    @GetMapping("getRoleById")
    public Response<SysRole> getRoleById(Long roleId) {
        return Response.rspData(sysRoleService.getById(roleId));
    }

    @ApiOperation("获取所有角色")
    @GetMapping("getAllRole")
    public Response<List<SysRole>> getRoleList(String roleName) {
        LambdaQueryWrapper<SysRole> qw = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(roleName)) {
            qw.like(SysRole::getRoleName, roleName);
        }
        return Response.rspData(sysRoleService.list(qw));
    }

    @ApiOperation("添加角色")
    @PostMapping("addRole")
    public Response addRole(@RequestBody SysRole sysRole) {
        String roleName = sysRole.getRoleName();
        String roleKey = sysRole.getRoleKey();
        if (StrUtil.isEmpty(roleName) || StrUtil.isEmpty(roleKey)) {
            return Response.error("角色名称和角色Key必填");
        }
        return Response.rspData(sysRoleService.save(sysRole));
    }

    @ApiOperation("更新角色信息")
    @PutMapping("updateRole")
    public Response<Boolean> updateRole(@RequestBody SysRole sysRole) {
        return Response.rspData(sysRoleService.updateById(sysRole));
    }

    @ApiOperation("根据ID删除角色")
    @PostMapping("deleteRole")
    public Response<Boolean> deleteRole(Long roleId) {
        return Response.rspData(sysRoleService.removeById(roleId));
    }
}
